﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cadcam7Moddel.Model;
using Cadcam7Moddel.Context;
using camcad7Home.CultureHelper;
namespace camcad7Home.Controllers
{
    public class TrangController : CultureController
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /Trang/
       public ActionResult jsonList()
        {

           string a = Request.Cookies["_culture"]["lang"];
           var objectitems = db.ObjectItems.ToList();
           List<TrangExt> list = new List<TrangExt>();
           foreach (var item in objectitems)
           {
               
               var cwithlang = db.Trang.Where(m => m.LangID == a&&m.ItemID==item.ID).FirstOrDefault();
               TrangExt tr = new TrangExt();
               if (cwithlang == null)
               {
                  
                   tr.ItemID = item.ID;
                   tr.Tieude = item.Name;
               }
               else {
                   tr.ItemID = cwithlang.ItemID;
                   tr.Tieude = cwithlang.Tieude ;
                   tr.DaDich = true;
                   tr.Hien = cwithlang.Hien;
                   tr.Hinh = cwithlang.Hinh;
                   tr.ID = cwithlang.ID;
                   tr.Luocxem = cwithlang.Luocxem;
                   tr.Ngaytao = cwithlang.Ngaytao;
                   tr.Noidung = cwithlang.Noidung;
                   tr.Thutu = cwithlang.Thutu;
                   tr.Tomtac = cwithlang.Tomtac;
                   tr.Trangchu = cwithlang.Trangchu;
                   tr.LangID = cwithlang.LangID;
               }
                list.Add(tr);
           }

           return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Trang/Details/5

        public ActionResult Details(int id = 0)
        {
            Trang trang = db.Trang.Find(id);
            if (trang == null)
            {
                return HttpNotFound();
            }
            return View(trang);
        }

        //
        // GET: /Trang/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Trang/Create
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Trang trang)
        {
            
            if (ModelState.IsValid)
            {
                trang.Noidung = HttpUtility.HtmlEncode(trang.Noidung);
                trang.Luocxem = 0;
                trang.Ngaytao = DateTime.Now;
                trang.LangID = Request.Cookies["_culture"]["lang"];
                ObjectItem item = new ObjectItem();
                item.Name = trang.Tieude;
                db.ObjectItems.Add(item);
                db.SaveChanges();
                trang.ItemID = item.ID;
                db.Trang.Add(trang);
                db.SaveChanges();
               
            
                return RedirectToAction("Index");
            }

            return View(trang);
        }

        //
        // GET: /Trang/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ObjectItem item = db.ObjectItems.Find(id);
            if (item == null)
            {
                return HttpNotFound();
            }
               string a = Request.Cookies["_culture"]["lang"];
            Trang trang = db.Trang.Where(m => m.ItemID == item.ID&&m.LangID==a).FirstOrDefault();
            if (trang == null)
            {
                trang = new Trang();
                trang.Tieude = item.Name;
                trang.ItemID = item.ID;
                trang.LangID = a;
               
            }
            else
            {
                trang.Noidung = HttpUtility.HtmlDecode(trang.Noidung);
            }
            trang.LangID = a;
            return View(trang);
        }

        //
        // POST: /Trang/Edit/5
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Trang trang)
        {
            if (ModelState.IsValid)
            {
                  string a = Request.Cookies["_culture"]["lang"];
                var tr = db.Trang.Where(m => m.ItemID == trang.ItemID && m.LangID == a).FirstOrDefault();
                trang.Noidung = HttpUtility.HtmlEncode(trang.Noidung);
                if (tr!=null)
                {
                    tr.Hien = trang.Hien;
                    tr.Hinh = trang.Hinh;
                    tr.Noidung = trang.Noidung;
                    tr.Thutu = trang.Thutu;
                    tr.Tieude = trang.Tieude;
                    tr.Tomtac = trang.Tomtac;
                    tr.Trangchu = trang.Trangchu;
                    db.SaveChanges();
                }
                else
                {
                    trang.ID = 0;
                    trang.Ngaytao = DateTime.Now;
                    db.Trang.Add(trang);
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            return View(trang);
        }

        //
        // GET: /Trang/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Trang trang = db.Trang.Find(id);
            if (trang == null)
            {
                return HttpNotFound();
            }
            return View(trang);
        }

        //
        // POST: /Trang/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Trang trang = db.Trang.Find(id);
            db.Trang.Remove(trang);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        public ActionResult ajDelete(int id)
        {
            Trang cauhinh = db.Trang.Where(m => m.ID == id).FirstOrDefault();
            if (cauhinh != null)
            {
                int itemid = cauhinh.ItemID;
                db.Trang.Remove(cauhinh);                
                //db.SaveChanges();
                db.SaveChanges();
                var others = db.Trang.Where(m => m.ItemID == itemid);
                if (others.Count()==0)
                {
                    var item = db.ObjectItems.Where(m => m.ID == itemid).FirstOrDefault();
                    db.ObjectItems.Remove(item);
                    db.SaveChanges();
                }
                return Json(new { success = true });
            }
            else
            {
                return Json(new { success = false });
            }
        }
        public ActionResult xemtintuc(int id)
        {
            var tin = (from n in db.Trang
                       where n.ID == id
                       select n).FirstOrDefault();
            return View(tin);
        }
    }
}